clear all
set more off, perm
set maxvar 120000
set varabbrev off
* ---------------------------------------------- *
global dir 	"`1'"
global Data 	$dir/Data
global Tables 	$dir/Tables
global Figures 	$dir/Figures
global Work 	$dir/Work
global Temp 	$dir/Temp
global Pseudo	$dir/Work/Temp
* ---------------------------------------------- *
* Compute dividend-to-price
import sas $Data/CRSP/msf.sas7bdat, clear case(lower)
keep permno permco date cfacpr cfacshr prc shrout retx ret
generate datem=mofd(date)
format date  %td
format datem %tm

tsset permno datem
generate Aret=1
generate Acap=1

forvalues x=0(1)11 {
	replace Aret=Aret*(1+L`x'.ret)
	replace Acap=Acap*(1+L`x'.retx)
}	
generate DP = Aret/Acap-1 
keep permno permco datem DP
* ************************************************
* street_mthly-- created read_compustat_qtly.do
merge 1:1 permno permco datem using $Work/street_mthly

tsset permno datem
gen eps=streetLTM/shsout
gen sps=  saleLTM/shsout

gen ge1Mo=ln( F1.streetLTM)-ln(streetLTM)
gen ge1Qr=ln( F3.streetLTM)-ln(streetLTM)
gen ge1Yr=ln(F12.streetLTM)-ln(streetLTM)
gen ge3Yr=ln(F36.streetLTM)-ln(streetLTM)
gen ge5Yr=ln(F60.streetLTM)-ln(streetLTM)

gen geLMo1=ln(streetLTM)-ln(L1.streetLTM )
gen geLQr1=ln(streetLTM)-ln(L3.streetLTM )
gen geLYr1=ln(streetLTM)-ln(L12.streetLTM)
gen geLYr3=ln(streetLTM)-ln(L36.streetLTM)
gen geLYr5=ln(streetLTM)-ln(L60.streetLTM)

gen gsLMo1=ln(saleLTM)-ln(L1.saleLTM )
gen gsLQr1=ln(saleLTM)-ln(L3.saleLTM )
gen gsLYr1=ln(saleLTM)-ln(L12.saleLTM)
gen gsLYr3=ln(saleLTM)-ln(L36.saleLTM)
gen gsLYr5=ln(saleLTM)-ln(L60.saleLTM)

gen gepsLMo1=ln(eps)-ln(L1.eps )
gen gepsLQr1=ln(eps)-ln(L3.eps )
gen gepsLYr1=ln(eps)-ln(L12.eps)
gen gepsLYr3=ln(eps)-ln(L36.eps)
gen gepsLYr5=ln(eps)-ln(L60.eps)

gen gspsLMo1=ln(sps)-ln(L1.sps)
gen gspsLQr1=ln(sps)-ln(L3.sps)
gen gspsLYr1=ln(sps)-ln(L12.sps)
gen gspsLYr3=ln(sps)-ln(L36.sps)
gen gspsLYr5=ln(sps)-ln(L60.sps)

drop if _merge==2
drop _merge sps eps
merge 1:1 permno permco datem using $Work/EBR_new_monthly

forvalues x=1(1)60  {
	quietly: replace  ret_mo`x'= ret_mo`x'-1
	quietly: replace retx_mo`x'=retx_mo`x'-1
}
tsset permno datem 
foreach var in qnic qcap {
	forvalues x=2(1)60  {
		local j=`x'-1
		quietly: replace `var'`x'=F1.`var'`j' if `var'`x'==.
	}
}	
drop SIZE
rename SI10 SIZE
keep if BOOK+LTG<.
tabulate _merge 
drop _merge
* *****************
gstats winsor beme g*1Mo g*1Qr g*Yr *STG* qnic_yr* DP, cuts(1 99) replace  
* *********************************************************
order *, sequential
replace beme=. if beme<0
* *********************************************************
rename mcap mcap0
tempfile block01
save "`block01'", replace
* *********************************************************
import sas using $Data/CRSP/msf.sas7bdat, clear 
rename *, lower
replace prc=abs(prc)
gen datem=mofd(date)
format datem %tm
keep permco permno datem cfacshr cfacpr shrout prc

merge 1:1 permco permno datem using $Work/street_mthly 
keep if _merge==3

generate eps=1000*streetLTM/(cfacshr*shrout)

tsset permno datem
generate L0eps=L3.eps
generate L1eps=L12.L0eps
generate L2eps=L24.L0eps

generate   e_p=L0eps/(prc/cfacshr)
generate cae_p=((L0eps+L1eps+L2eps)/3)/(prc/cfacshr)
keep permco permno datem cae_p e_p cfacpr
* ************************************************
merge 1:1 permco permno datem using "`block01'"
drop if _merge==1
drop _merge  

generate  medest_p=medest12/(abs(prc)/cfacpr)
* *****************************************************
save $Temp/tmp_portfolio_monthly, replace
* ************************************************
* msf_adjusted.dta generated by read_crsp.do
use $Work/msf_adjusted, clear
tsset permno datem

gen  LretMo1=1+ret
gen  LretQr1=1
gen  LretYr1=1
gen  LretYr3=1
gen  LretYr5=1

forvalues x=0(1)2 {
	replace   LretQr1= LretQr1*(1+L`x'.ret)
}
forvalues x=0(1)11 {
	replace   LretYr1= LretYr1*(1+L`x'.ret)
}
forvalues x=0(1)35 {
	replace   LretYr3= LretYr3*(1+L`x'.ret)
}
forvalues x=0(1)59 {
	replace   LretYr5= LretYr5*(1+L`x'.ret)
}
keep permco permno datem Lret*
drop if year(dofm(datem))<1981
* ************************************************
merge 1:1 permco permno datem using $Temp/tmp_portfolio_monthly
drop if _merge==1
drop _merge
save $Temp/tmp_portfolio_monthly, replace
* ************************************************
foreach PORTF in BOOK INV PROF SIZE MOME {
foreach var in qped qslo qnic qruf {			

use using $Temp/tmp_portfolio_monthly, clear
generate portf=1+int((`PORTF'-1)/2)    
drop retx*
drop ret_yr*
keep if `var'1<.

tsset permco datem
gen QLRetMo1=(1+L1.qnic1)
gen QLRetQr1=(1+L3.qnic1)*(1+L3.qnic2)*(1+L3.qnic3)
gen QLRetYr1=L12.qnic_yr1
gen QLRetYr2=L24.qnic_yr1
gen QLRetYr3=L36.qnic_yr1
gen QLRetYr4=L48.qnic_yr1
gen QLRetYr5=L60.qnic_yr1

forvalues x=1(1)60 {
gen     retA`x'=ret`x'
replace  ret`x'   =. 	if `var'`x'==.
replace  ret_mo`x'=. 	if `var'`x'==.
replace `var'`x'=. 	if  ret`x' ==.

rename `var'`x' squiggle`x'
}
* *******
preserve
collapse (mean) squig*, by(datem)
scalar alfa= 0.97749304

gen qretMo1=1+squiggle1
gen qretQr1=1
forvalues x=1(1)3 {
replace  qretQr1=qretQr1*(1+squiggle`x')
}
gen qretMo6=1
forvalues x=1(1)6 {
replace  qretMo6=qretMo6*(1+squiggle`x')
}

forvalues x=1(1)5 {
generate qretYr`x'=1
}
forvalues x=1(1)12 {
replace  qretYr1=qretYr1*(1+squiggle`x')
}
forvalues x=13(1)24 {
replace  qretYr2=qretYr2*(1+squiggle`x')
}
forvalues x=25(1)36 {
replace  qretYr3=qretYr3*(1+squiggle`x')
}
forvalues x=37(1)48 {
replace  qretYr4=qretYr4*(1+squiggle`x')
}
forvalues x=49(1)60 {
replace  qretYr5=qretYr5*(1+squiggle`x')
}
gen cumQMktMo1=ln(qretMo1) 
gen cumQMktQr1=ln(qretQr1) 
gen cumQMktMo6=ln(qretMo6) 
gen cumQMktYr1=ln(qretYr1) 
gen cumQMktYr3=ln(qretYr1)+alfa*ln(qretYr2)+alfa^2*ln(qretYr3) 
gen cumQMktYr5=ln(qretYr1)+alfa*ln(qretYr2)+alfa^2*ln(qretYr3)+alfa^3*ln(qretYr4)+alfa^4*ln(qretYr5)
keep datem cum*

tempfile qmkt
save "`qmkt'", replace
* *******
restore
collapse (mean) ret* squig* beme gs* ge* FE* cp* LTG* FGE* L12ret Lret* *STG* qnic_yr* DP cae_p e_p medest_p QLRet*, by(portf datem)
gstats winsor cae_p e_p medest_p QLRet*, cuts(1 99) replace 
* i don't need to do this for DP
foreach log of varlist cae_p e_p medest_p  {
	egen min=min(`log')
	gen ln`log'=ln(`log'+min+1)
	drop min
} 
gen lndp=ln(DP)
* *******************************
merge m:1 datem using $Work/mktrf
keep if _merge==3
drop _merge

scalar alfa= 0.97749304

gen  retMo1=1+ret1
gen AretMo1=1+retA1
gen NretMo1=1+ret_mo1
gen qretMo1=1+squiggle1

gen  retQr1=1
gen NretQr1=1
gen AretQr1=1
gen qretQr1=1
forvalues x=1(1)3 {
replace   retQr1= retQr1*(1+ret`x')
replace  AretQr1=AretQr1*(1+retA`x')
replace  NretQr1=NretQr1*(1+ret_mo`x')
replace  qretQr1=qretQr1*(1+squiggle`x')
}
gen qretMo6=1
forvalues x=1(1)6 {
replace  qretMo6=qretMo6*(1+squiggle`x')
}
forvalues x=1(1)5 {
gen  retYr`x'=1
gen AretYr`x'=1
gen NretYr`x'=1
gen qretYr`x'=1
}
forvalues x=1(1)12 {
replace   retYr1= retYr1*(1+ret`x')
replace  AretYr1=AretYr1*(1+retA`x')
replace  NretYr1=NretYr1*(1+ret_mo`x')
replace  qretYr1=qretYr1*(1+squiggle`x')
}
forvalues x=13(1)24 {
replace   retYr2= retYr2*(1+ret`x')
replace  AretYr2=AretYr2*(1+retA`x')
replace  NretYr2=NretYr2*(1+ret_mo`x')
replace  qretYr2=qretYr2*(1+squiggle`x')
}
forvalues x=25(1)36 {
replace   retYr3= retYr3*(1+ret`x')
replace  AretYr3=AretYr3*(1+retA`x')
replace  NretYr3=NretYr3*(1+ret_mo`x')
replace  qretYr3=qretYr3*(1+squiggle`x')
}
forvalues x=37(1)48 {
replace   retYr4= retYr4*(1+ret`x')
replace  AretYr4=AretYr4*(1+retA`x')
replace  NretYr4=NretYr4*(1+ret_mo`x')
replace  qretYr4=qretYr4*(1+squiggle`x')
}
forvalues x=49(1)60 {
replace   retYr5= retYr5*(1+ret`x')
replace  AretYr5=AretYr5*(1+retA`x')
replace  NretYr5=NretYr5*(1+ret_mo`x')
replace  qretYr5=qretYr5*(1+squiggle`x')
}
gen cumRetMo1 =ln( retMo1)
gen cumARetMo1=ln(AretMo1)
gen cumNRetMo1=ln(NretMo1)
gen cumQRetMo1=ln(qretMo1) 

gen cumRetQr1 =ln( retQr1)
gen cumARetQr1=ln(AretQr1)
gen cumNRetQr1=ln(NretQr1)
gen cumQRetQr1=ln(qretQr1) 

gen cumQRetMo6=ln(qretMo6) 

gen  cumRetYr1=ln( retYr1)
gen cumARetYr1=ln(AretYr1)
gen cumNRetYr1=ln(NretYr1)
gen cumQRetYr1=ln(qretYr1) 

gen  cumRetYr3=ln( retYr1)+alfa*ln( retYr2)+alfa^2*ln( retYr3) 
gen cumARetYr3=ln(AretYr1)+alfa*ln(AretYr2)+alfa^2*ln(AretYr3)
gen cumNRetYr3=ln(NretYr1)+alfa*ln(NretYr2)+alfa^2*ln(NretYr3)
gen cumQRetYr3=ln(qretYr1)+alfa*ln(qretYr2)+alfa^2*ln(qretYr3) 

gen  cumRetYr5=ln( retYr1)+alfa*ln( retYr2)+alfa^2*ln( retYr3)+alfa^3*ln( retYr4)+alfa^4*ln( retYr5)
gen cumARetYr5=ln(AretYr1)+alfa*ln(AretYr2)+alfa^2*ln(AretYr3)+alfa^3*ln(AretYr4)+alfa^4*ln(AretYr5)
gen cumNRetYr5=ln(NretYr1)+alfa*ln(NretYr2)+alfa^2*ln(NretYr3)+alfa^3*ln(NretYr4)+alfa^4*ln(NretYr5)
gen cumQRetYr5=ln(qretYr1)+alfa*ln(qretYr2)+alfa^2*ln(qretYr3)+alfa^3*ln(qretYr4)+alfa^4*ln(qretYr5)

gen cumQLRetMo1=ln(QLRetMo1)
gen cumQLRetQr1=ln(QLRetQr1)
gen cumQLRetYr1=ln(QLRetYr1)
gen cumQLRetYr3=ln(QLRetYr1)+alfa*ln(QLRetYr2)+alfa^2*ln(QLRetYr3)
gen cumQLRetYr5=ln(QLRetYr1)+alfa*ln(QLRetYr2)+alfa^2*ln(QLRetYr3)+alfa^3*ln(QLRetYr4)+alfa^4*ln(QLRetYr5)
drop QLRet*
forvalues t=1(1)5 {
	gen lnqnic_yr`t'=ln(qnic_yr`t')
}
* ************************************************************
local Z     "cum*Mo1 cum*Qr1 cum*Mo6 cum*Yr1 cum*Yr3 cum*Yr5"
local Z "`Z' ge1Mo ge1Qr ge1Yr ge3Yr ge5Yr"
local Z "`Z' geLMo1 geLQr1 geLYr1 geLYr3 geLYr5 gepsLMo1 gepsLQr1 gepsLYr1 gepsLYr3 gepsLYr5"
local Z "`Z' gsLMo1 gsLQr1 gsLYr1 gsLYr3 gsLYr5 gspsLMo1 gspsLQr1 gspsLYr1 gspsLYr3 gspsLYr5"  
local Z "`Z' FE1-FE5 cp1-cp5 LTG LTGYr* STG2"	 
local Z "`Z' FGE1-FGE3 L12ret Lret* lnqnic_yr* DP lndp lncae_p lne_p e_p lnmedest_p"
keep datem portf `Z'

preserve
keep if portf==1 | portf==5 
foreach v of varlist `Z' {
gen `v'low =`v' if portf==1
gen `v'high=`v' if portf==5
bysort datem: egen c`v' = count(`v') 
replace `v'=. if c`v'<2
}
gen DP5=DP if portf==5
gen DP1=DP if portf==1

gen EP5=e_p if portf==5
gen EP1=e_p if portf==1

drop LTGYr*low LTGYr*high lnqnic*high lnqnic*low Lret*high Lret*low
drop lndp*low  lncae_p*low  lne_p*low  lnmedest_p*low
drop lndp*high lncae_p*high lne_p*high lnmedest_p*high
 
foreach name of varlist `Z' {
replace `name'=-`name' if portf==5 & index("`PORTF'","INV")+index("`PORTF'","BETA")+index("`PORTF'","SIZE")+index("`PORTF'","RNK")==1
replace `name'=-`name' if portf==1 & index("`PORTF'","INV")+index("`PORTF'","BETA")+index("`PORTF'","SIZE")==0
}
collapse (sum)  `Z' DP1 DP5 EP5 EP1 (lastnm) *low *high, by(datem)
foreach name of varlist  `Z' DP1 DP5 EP5 EP1 {
replace `name'=. if `name'==0
}
drop DP
generate portf=6
generate lnDP=ln(DP5)-ln(DP1) if index("`PORTF'","BOOK")+index("`PORTF'","PROF")+index("`PORTF'","MOME")==1
replace  lnDP=ln(DP1)-ln(DP5) if index("`PORTF'","INV" )+index("`PORTF'","SIZE")+index("`PORTF'","BETA")==1
generate lnEP=ln(EP5)-ln(EP1) if index("`PORTF'","BOOK")+index("`PORTF'","PROF")+index("`PORTF'","MOME")==1
replace  lnEP=ln(EP1)-ln(EP5) if index("`PORTF'","INV" )+index("`PORTF'","SIZE")+index("`PORTF'","BETA")==1

foreach t in Mo1 Qr1 Mo6 Yr1 Yr3 Yr5 {
	gen spread`t'=cumQRet`t'
}
save $Temp/portfolio_spread_`PORTF'_`var', replace
restore
* *******
generate lnDP=ln(DP ) if portf!=6
generate lnEP=ln(e_p) if portf!=6
append using $Temp/portfolio_spread_`PORTF'_`var'
drop spread*
merge m:1 datem using $Temp/portfolio_spread_`PORTF'_`var', keepusing(datem spread*)
drop if _merge==2
drop _merge
* ************************************************************
merge m:1 datem using "`qmkt'"
drop if _merge==2
drop _merge
save $Work/portfolio_`PORTF'_`var', replace
}
}

